package com.gitee.flyzing.jxt.component;

import com.gitee.flyzing.jxt.CommonUtil;
import org.apache.poi.ss.usermodel.Cell;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JXTEachItemCell extends JXTCell {
    @Override
    public void parseCell(Cell cell, Object datas, Map params) {
        if (params != null) {
            String itemName = (String) params.get("itemName");
            if (CommonUtil.isNotNull(itemName)) {
                String cellValue = cell.getStringCellValue();
                //匹配值表达式
                String exp = "\\{\\{\\s*(\\S*)\\s*\\}\\}";
                Pattern p = Pattern.compile(exp);
                Matcher m = p.matcher(cellValue);
                if (m.find()) {
                    String propertyNames = m.group(1);
                    if (propertyNames.indexOf(itemName) == 0 && propertyNames.indexOf(".") > -1) { //是item属性
                        String itemPropertyName = propertyNames.substring(propertyNames.indexOf(".") + 1);
                        Object itemProperty = CommonUtil.getPropertyValue(datas, itemPropertyName);
                        if (itemProperty != null) {
                            setCellValue(itemProperty.toString());
                        }
                    }
                }
            }
        }
    }
}
